package Transaksi;

import Barang.Barang;
import Barang.BarangForm;
import Kasir.Kasir;
import Kasir.LoginForm;
import KategoriBarang.KategoriForm;
import Koneksi.Koneksi;
import java.awt.Font;
import java.util.*;
import java.util.Date;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class TransaksiForm extends javax.swing.JFrame {

    private int row = 0;
    private int no = 1;
    private Kasir kasir;

    public TransaksiForm() {
        initComponents();
        //jenny
        namaField.setEditable(false);
        hargaField.setEditable(false);
        discField.setEditable(false);
        totalField.setEditable(false);
        kodeBarangField.setEditable(false);
        // jenny
        GregorianCalendar gc = new GregorianCalendar();
        DateChooser.setDate(gc.getTime());


        noNotaField.setText("SC-HR-" + ((new Date().getYear() + 1900) + "").substring(2)
                + (new Date().getMonth() + 1) + "-" + nomorNota());

        noNotaField.setEditable(false);

        urutField.setText(" " + no);


        ActionListener taskPerformer = new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                waktu.setFont(new Font("jam", 1, 17));
                String nolJam = "";
                String nolMenit = "";
                String nolDetik = "";

                Date dt = new Date();

                int nilai_jam = dt.getHours();
                int nilai_menit = dt.getMinutes();
                int nilai_detik = dt.getSeconds();

                if (nilai_jam <= 9) {

                    nolJam = "0";
                }

                if (nilai_menit <= 9) {

                    nolMenit = "0";
                }

                if (nilai_detik <= 9) {

                    nolDetik = "0";
                }

                String jam = nolJam + Integer.toString(nilai_jam);
                String menit = nolMenit + Integer.toString(nilai_menit);
                String detik = nolDetik + Integer.toString(nilai_detik);

                waktu.setText("  " + jam + " : " + menit + " : " + detik + "  ");
            }
        };

        new Timer(1000, taskPerformer).start();


    }

    public void setKasir(Kasir kasir) {
        this.kasir = kasir;
        kasirField.setText(kasir.getId_Kasir());
    }

    public final String nomorNota() {
        try {
            noNotaByBulan();
            String sql = "select * from NOTA";
            Connection konek = new Koneksi().getConnection();
            Statement stat = konek.createStatement();
            ResultSet rs = stat.executeQuery(sql);
            rs.next();


            String result = rs.getString(1);
            int no = Integer.parseInt(result);
            no++;

            sql = "" + no;
            while (sql.length() <= 3) {
                sql = "0" + sql;
            }

            sql = "update NOTA set noNota = '" + sql + "' where noNota = '" + result + "'";
            stat = konek.createStatement();
            stat.executeUpdate(sql);
            konek.close();

            return result;

        } catch (SQLException ex) {
            return "";
        }
    }

    public final void noNotaByBulan() {
        try {
            String sql = "select * from NOTA";
            Connection konek = new Koneksi().getConnection();
            Statement stat = konek.createStatement();
            ResultSet rs = stat.executeQuery(sql);
            rs.next();

            String resultMonth = rs.getString(2);
            int tgl = Integer.parseInt(resultMonth);
            String loop = "0001";

            if (tgl != new Date().getMonth() + 1) {
                sql = "update NOTA set noNota = '" + loop + "',BULAN='" + (new Date().getMonth() + 1) + "'";
                stat = konek.createStatement();
                stat.executeUpdate(sql);
                konek.close();
            }
        } catch (SQLException ex) {
        }
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jDesktopPane1 = new javax.swing.JDesktopPane();
        jPanel1 = new javax.swing.JPanel();
        tglLabel = new javax.swing.JLabel();
        kasirLabel = new javax.swing.JLabel();
        kasirField = new javax.swing.JTextField();
        kodeBarangField = new javax.swing.JTextField();
        totalByrField = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        JamLabel = new javax.swing.JLabel();
        waktu = new javax.swing.JLabel();
        judulLabel = new javax.swing.JLabel();
        DateChooser = new com.toedter.calendar.JDateChooser();
        jPanel2 = new javax.swing.JPanel();
        noLabel = new javax.swing.JLabel();
        kodeBarangLabel = new javax.swing.JLabel();
        namaBarangLabel = new javax.swing.JLabel();
        hargaLabel = new javax.swing.JLabel();
        jumlahLabel = new javax.swing.JLabel();
        discLabel = new javax.swing.JLabel();
        totalLabel = new javax.swing.JLabel();
        urutField = new javax.swing.JTextField();
        kodeField = new javax.swing.JTextField();
        namaField = new javax.swing.JTextField();
        hargaField = new javax.swing.JTextField();
        jmlField = new javax.swing.JTextField();
        discField = new javax.swing.JTextField();
        totalField = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabelJual = new javax.swing.JTable();
        detailLabel = new javax.swing.JLabel();
        totalPembayaranLabel = new javax.swing.JLabel();
        totalPembayarnField = new javax.swing.JTextField();
        totalYgDibayarkanLabel = new javax.swing.JLabel();
        totalYgDibayarkanField = new javax.swing.JTextField();
        sisaLabel = new javax.swing.JLabel();
        sisaField = new javax.swing.JTextField();
        simpanButton = new javax.swing.JButton();
        hitungButton = new javax.swing.JButton();
        notaLabel = new javax.swing.JLabel();
        noNotaField = new javax.swing.JTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        tabelKategoriItem = new javax.swing.JMenuItem();
        tabelBarangItem = new javax.swing.JMenuItem();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jButton3.setText("OK");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        jDesktopPane1.setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));

        tglLabel.setText("Tanggal");

        kasirLabel.setText("Kasir ");

        kasirField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kasirFieldActionPerformed(evt);
            }
        });

        judulLabel.setFont(new java.awt.Font("Rod", 3, 18));
        judulLabel.setText("FORM TRANSAKSI HARI'S MINIMARKET");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(582, 582, 582)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(waktu)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(JamLabel)
                                .addGap(57, 57, 57)
                                .addComponent(jLabel14))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(tglLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(DateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(kasirLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(kasirField, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(231, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(kodeBarangField, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 275, Short.MAX_VALUE)
                .addComponent(totalByrField, javax.swing.GroupLayout.PREFERRED_SIZE, 323, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(judulLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 397, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(463, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(judulLabel)
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(JamLabel)
                    .addComponent(waktu))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tglLabel)
                    .addComponent(DateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(kasirLabel)
                        .addComponent(kasirField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(kodeBarangField, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(totalByrField, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(39, Short.MAX_VALUE))
        );

        jPanel1.setBounds(0, 0, 870, 190);
        jDesktopPane1.add(jPanel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jPanel2.setBackground(new java.awt.Color(51, 255, 255));

        noLabel.setText("No");

        kodeBarangLabel.setText("Kode Barang");

        namaBarangLabel.setText("Nama Barang");

        hargaLabel.setText("Harga Satuan");

        jumlahLabel.setText("Jumlah item");

        discLabel.setText("Disc (Rp)");

        totalLabel.setText("Total");

        urutField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                urutFieldActionPerformed(evt);
            }
        });

        kodeField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kodeFieldActionPerformed(evt);
            }
        });
        kodeField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                kodeFieldKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                kodeFieldKeyReleased(evt);
            }
        });

        namaField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                namaFieldKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 839, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 171, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(urutField, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addComponent(noLabel)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jumlahLabel)
                            .addComponent(jmlField, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(kodeField, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(kodeBarangLabel))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(namaBarangLabel)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(namaField, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(30, 30, 30)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(hargaField, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(hargaLabel))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(discField, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(discLabel))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(totalLabel)
                                    .addComponent(totalField, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(noLabel)
                    .addComponent(namaBarangLabel)
                    .addComponent(jumlahLabel)
                    .addComponent(kodeBarangLabel)
                    .addComponent(hargaLabel)
                    .addComponent(discLabel)
                    .addComponent(totalLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(urutField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jmlField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(kodeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(namaField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(hargaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(discField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(totalField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(534, 534, 534)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBounds(0, 180, 850, 80);
        jDesktopPane1.add(jPanel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jPanel4.setBackground(new java.awt.Color(204, 204, 204));

        tabelJual.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "No", "Jumlah", "Kode Barang", "Nama Barang", "Harga Satuan", "Disc (Rp)", " Total"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tabelJual);

        detailLabel.setText("Rincian");

        totalPembayaranLabel.setText("Total Pembayaran Rp");

        totalPembayarnField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                totalPembayarnFieldActionPerformed(evt);
            }
        });

        totalYgDibayarkanLabel.setText("Total yang dibayarkan Rp");

        totalYgDibayarkanField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                totalYgDibayarkanFieldActionPerformed(evt);
            }
        });

        sisaLabel.setText("Sisa Rp");

        sisaField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sisaFieldActionPerformed(evt);
            }
        });

        simpanButton.setText("Simpan");
        simpanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simpanButtonActionPerformed(evt);
            }
        });

        hitungButton.setText("Hitung");
        hitungButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hitungButtonActionPerformed(evt);
            }
        });

        notaLabel.setText("Nomor Nota");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(detailLabel)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 744, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(hitungButton)))
                .addGap(23, 23, 23))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(simpanButton))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(notaLabel)
                        .addGap(18, 18, 18)
                        .addComponent(noNotaField, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 206, Short.MAX_VALUE)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(totalPembayaranLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(totalYgDibayarkanLabel)
                            .addComponent(sisaLabel))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(sisaField, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                                .addComponent(totalYgDibayarkanField))
                            .addComponent(totalPembayarnField, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(124, 124, 124))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(hitungButton)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(detailLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(totalPembayarnField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(totalPembayaranLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(noNotaField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(notaLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(totalYgDibayarkanLabel)
                    .addComponent(totalYgDibayarkanField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sisaLabel)
                    .addComponent(sisaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(simpanButton)
                .addGap(319, 319, 319))
        );

        jPanel4.setBounds(0, 260, 850, 390);
        jDesktopPane1.add(jPanel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        getContentPane().add(jDesktopPane1);
        jDesktopPane1.setBounds(0, 0, 850, 540);

        jMenu1.setText("File");
        jMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu1ActionPerformed(evt);
            }
        });

        jMenuItem1.setText("Logout");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Modify");

        tabelKategoriItem.setText("Tabel Kategori");
        tabelKategoriItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tabelKategoriItemActionPerformed(evt);
            }
        });
        jMenu2.add(tabelKategoriItem);

        tabelBarangItem.setText("Tabel Barang");
        tabelBarangItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tabelBarangItemActionPerformed(evt);
            }
        });
        jMenu2.add(tabelBarangItem);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-865)/2, (screenSize.height-597)/2, 865, 597);
    }// </editor-fold>//GEN-END:initComponents

    private void kodeFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_kodeFieldKeyPressed
        int item = Integer.parseInt(jmlField.getText().toString());

        if (evt.getKeyCode() == evt.VK_ENTER) {
            DefaultTableModel model = (DefaultTableModel) tabelJual.getModel();
            Object[] ob = new Object[7];
            ob[0] = urutField.getText();
            ob[1] = jmlField.getText();
            ob[2] = kodeField.getText();
            ob[3] = namaField.getText();
            ob[4] = hargaField.getText();
            ob[5] = discField.getText();
            ob[6] = totalField.getText();

            model.insertRow(model.getRowCount(), ob);


            //jenny

            jmlField.setText("");
            kodeField.setText("");
            namaField.setText("");
            hargaField.setText("");
            discField.setText("");
            totalField.setText("");

            no++;
            urutField.setText("" + (no));
            row++;
            try {
                String sql = "select Jumlah_Stock_Awal from barang where Kode_Barang = '" + kodeBarangField.getText() + "'";
                Connection konek = new Koneksi().getConnection();
                Statement stat = konek.createStatement();
                ResultSet rs = stat.executeQuery(sql);
                if (rs.next()) {
                    int jum = rs.getInt("Jumlah_Stock_Awal");
                    int current = jum - item;

                    sql = "update barang set Jumlah_Stock_Awal = '" + current + "' where Kode_Barang = '" + kodeBarangField.getText() + "'";
                    stat = konek.createStatement();
                    stat.executeUpdate(sql);
                    konek.close();
                }
            } catch (SQLException ex) {
            }

    }//GEN-LAST:event_kodeFieldKeyPressed
    }

    private void kodeFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_kodeFieldKeyReleased
        kodeBarangField.setText(kodeField.getText());
        kodeBarangField.setFont(new Font("kode", 1, 40));

        try {
            Barang barang = new Barang();
            String kode = kodeField.getText();
            barang.setKodeBarang(kode);


            barang = TransaksiKontrol.getKoneksiKontrol().cariBarang(barang);
            String nama = barang.getNamaBarang();
            Double hargaJual = barang.getHargaJual();


            namaField.setText(nama);
            hargaField.setText(String.valueOf(hargaJual));

            int jmlItem = Integer.parseInt(jmlField.getText().toString());
            double harga = Double.parseDouble(hargaField.getText());
            TransaksiKontrol transaksiKontrol = new TransaksiKontrol();
            Double disc = transaksiKontrol.hitungDiskon(jmlItem, harga);
            Double totalPerBarang = transaksiKontrol.hitungTotal(jmlItem, harga);

            discField.setText(String.valueOf(disc));
            totalField.setText(String.valueOf(totalPerBarang));

        } catch (Exception e) {
            Logger.getLogger(TransaksiForm.class.getName()).log(Level.SEVERE, null, e);

        }


    }//GEN-LAST:event_kodeFieldKeyReleased

    private void namaFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_namaFieldKeyReleased
    }//GEN-LAST:event_namaFieldKeyReleased

    private void totalPembayarnFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_totalPembayarnFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_totalPembayarnFieldActionPerformed

    private void totalYgDibayarkanFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_totalYgDibayarkanFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_totalYgDibayarkanFieldActionPerformed

    private void sisaFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sisaFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_sisaFieldActionPerformed

    private void tabelKategoriItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tabelKategoriItemActionPerformed
        // TODO add your handling code here:
        new KategoriForm().setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_tabelKategoriItemActionPerformed

    private void tabelBarangItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tabelBarangItemActionPerformed
        // TODO add your handling code here:
        new BarangForm().setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_tabelBarangItemActionPerformed

    private void hitungButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hitungButtonActionPerformed
        double total = 0;
        for (int i = 0; i < tabelJual.getRowCount(); i++) {
            total += Double.parseDouble(tabelJual.getValueAt(i, 6).toString());
        }
        totalPembayarnField.setText("" + total);
        totalByrField.setText("" + total);
        totalByrField.setFont(new Font("nama", 1, 40));

        TransaksiKontrol transaksiKontrol = new TransaksiKontrol();
        double pembayaran = Double.parseDouble(JOptionPane.showInputDialog(null, "Jumlah yang dibayarkan"));
        Double sisa = transaksiKontrol.hitungSisa(pembayaran, total);
        totalYgDibayarkanField.setText(String.valueOf(pembayaran));
        sisaField.setText(String.valueOf(sisa));

        totalByrField.setText("" + sisa);
        totalByrField.setFont(new Font("nama", 1, 40));
    }//GEN-LAST:event_hitungButtonActionPerformed

    private void simpanDetailBarang() throws SQLException {
        for (int i = 0; i < tabelJual.getRowCount(); i++) {
            String noNota = noNotaField.getText();
            java.util.Date dtTransaksi = DateChooser.getDate();
            SimpleDateFormat spdt = new SimpleDateFormat("yyyy-MM-dd");
            String tglTransaksi = spdt.format(dtTransaksi);
            java.sql.Date tglTran = java.sql.Date.valueOf(tglTransaksi);
            String idKasir = kasirField.getText();
            int item = Integer.parseInt(tabelJual.getValueAt(i, 1).toString());
            String kode = tabelJual.getValueAt(i, 2).toString();
            String nama = tabelJual.getValueAt(i, 3).toString();
            Double harga = Double.parseDouble(tabelJual.getValueAt(i, 4).toString());
            Double disc = Double.parseDouble(tabelJual.getValueAt(i, 5).toString());
            Double total = Double.parseDouble(tabelJual.getValueAt(i, 6).toString());

            try {
                String sql = "select * from DetailPenjualanBarang";
                Connection konek = new Koneksi().getConnection();
                Statement stat = konek.createStatement();
                ResultSet rs = stat.executeQuery(sql);

                rs.next();

                sql = "insert into DetailPenjualanBarang (No_Nota,Tgl_Transaksi,Id_Kasir,Jumlah_Item,Kode_Barang,"
                        + "Nama_Barang,Harga_Satuan,Discount,Total)"
                        + "values "
                        + "('" + noNota + "'," + "SYSDATE" + ",'" + idKasir + "','" + item + "','" + kode + "','" + nama + "',"
                        + harga + "," + disc + "," + total + ")";

                stat = konek.createStatement();
                stat.executeUpdate(sql);
                //JOptionPane.showMessageDialog(null, "Error");

            } catch (SQLException ex) {
            }
        }
    }

    private void kasirFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kasirFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_kasirFieldActionPerformed

    private void urutFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_urutFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_urutFieldActionPerformed

    private void kodeFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kodeFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_kodeFieldActionPerformed

    private void simpanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simpanButtonActionPerformed
        try {
            String noNota = noNotaField.getText();
            String idKasir = kasirField.getText();
            Double total = Double.parseDouble(totalByrField.getText());
            java.util.Date dtTransaksi = DateChooser.getDate();
            SimpleDateFormat spdt = new SimpleDateFormat("yyyy-MM-dd");
            String tglTransaksi = spdt.format(dtTransaksi);
            java.sql.Date tglTran = java.sql.Date.valueOf(tglTransaksi);

            Transaksi trans = new Transaksi();
            trans.setNomor_Nota(noNota);
            trans.setId_Kasir(idKasir);
            trans.setTotal_Pembayaran(total);
            trans.setTgl_Transaksi(tglTran);

            TransaksiKontrol transKontrol = TransaksiKontrol.getKoneksiKontrol();
            transKontrol.tambahTransaksi(trans);
            simpanDetailBarang();
            JOptionPane.showMessageDialog(this, "Nomor Nota" + noNota + " berhasil disimpan");
        } catch (SQLException ex) {
            Logger.getLogger(TransaksiForm.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this, "transaksi gagal ditambah.");
        }
    }//GEN-LAST:event_simpanButtonActionPerformed

    private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenu1ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:

        new LoginForm().setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TransaksiForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.toedter.calendar.JDateChooser DateChooser;
    private javax.swing.JLabel JamLabel;
    private javax.swing.JLabel detailLabel;
    private javax.swing.JTextField discField;
    private javax.swing.JLabel discLabel;
    private javax.swing.JTextField hargaField;
    private javax.swing.JLabel hargaLabel;
    private javax.swing.JButton hitungButton;
    private javax.swing.JButton jButton3;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jmlField;
    private javax.swing.JLabel judulLabel;
    private javax.swing.JLabel jumlahLabel;
    private javax.swing.JTextField kasirField;
    private javax.swing.JLabel kasirLabel;
    private javax.swing.JTextField kodeBarangField;
    private javax.swing.JLabel kodeBarangLabel;
    private javax.swing.JTextField kodeField;
    private javax.swing.JLabel namaBarangLabel;
    private javax.swing.JTextField namaField;
    private javax.swing.JLabel noLabel;
    private javax.swing.JTextField noNotaField;
    private javax.swing.JLabel notaLabel;
    private javax.swing.JButton simpanButton;
    private javax.swing.JTextField sisaField;
    private javax.swing.JLabel sisaLabel;
    private javax.swing.JMenuItem tabelBarangItem;
    private javax.swing.JTable tabelJual;
    private javax.swing.JMenuItem tabelKategoriItem;
    private javax.swing.JLabel tglLabel;
    private javax.swing.JTextField totalByrField;
    private javax.swing.JTextField totalField;
    private javax.swing.JLabel totalLabel;
    private javax.swing.JLabel totalPembayaranLabel;
    private javax.swing.JTextField totalPembayarnField;
    private javax.swing.JTextField totalYgDibayarkanField;
    private javax.swing.JLabel totalYgDibayarkanLabel;
    private javax.swing.JTextField urutField;
    private javax.swing.JLabel waktu;
    // End of variables declaration//GEN-END:variables
}
